# -*- coding: utf-8 -*-
import os,re,time
import base64
import xml.etree.cElementTree as ETree

class Element(object):
	def __init__(self):
		self.tempFile = os.getcwd()
		self.pattern  = re.compile(r"\d+")

	def __uidump(self):
		os.popen("adb shell uiautomator dump /data/local/tmp/uidump.xml")
		time.sleep(1)
		os.popen("adb pull /data/local/tmp/uidump.xml " + self.tempFile)
		time.sleep(2)

	def __element(self, attrib, name):
		self.__uidump()
		tree = ETree.ElementTree(file=self.tempFile + "\\uidump.xml")
		treeIter = tree.iter(tag="node")
		for elem in treeIter:
			if elem.attrib[attrib] == name:
				bounds = elem.attrib["bounds"]
				coord  = self.pattern.findall(bounds)
				xpoint = (int(coord[2]) - int(coord[0])) / 2.0 + int(coord[0])
				ypoint = (int(coord[3]) - int(coord[1])) / 2.0 + int(coord[1])
				return xpoint, ypoint

	def __elements(self, attrib, name):
		list = []
		self.__uidump()
		tree = ET.ElementTree(file=self.tempFile + "\\uidump.xml")
		treeIter = tree.iter(tag="node")
		for elem in treeIter:
			if elem.attrib[attrib] == name:
				bounds = elem.attrib["bounds"]
				coord = self.pattern.findall(bounds)
				xpoint = (int(coord[2]) - int(coord[0])) / 2.0 + int(coord[0])
				ypoint = (int(coord[3]) - int(coord[1])) / 2.0 + int(coord[1])
				list.append((xpoint, ypoint))
		return list
	def findElementByName(self, name):
		return self.__element("text", name)

	def findElementsByName(self, name):
		return self.__elements("text", name)

	def findElementByClass(self, clsname):
		return self.__element("class", clsname)

	def findElementsByClass(self, clsname):
		return self.__elements("class", clsname)

	def findElementById(self, id):
		return self.__element("resource-id", id)

	def findElementsById(self, id):
		return self.__elements("resource-id", id)

class Event(object):
	def __init__(self):
		pass
		#process = os.popen("adb connect 127.0.0.1:7555")
		#print(process.read())

	def touch(self, dx, dy):
		os.popen("adb shell input tap %s %s" % (str(dx), str(dy)))
		time.sleep(1)

	def dbtouch(self, dx, dy):
		os.popen("adb shell input tap %s %s && adb shell input tap %s %s" % (str(dx), str(dy), str(dx), str(dy)))

	def swipe(self, dx, dy, dx1, dy1):
		os.popen("adb shell input swipe 540 1300 540 500 100")

	def text(self, text):
		#os.popen("adb shell input keyevent 82")
		charsb64 = str(base64.b64encode(text.encode('utf-8')))[1:]
		os.popen("adb shell am broadcast -a ADB_INPUT_B64 --es msg %s" %charsb64)
		#os.popen("adb shell input text %s" % text)
		#os.popen("adb shell input keyevent 66")

	def back(self):
		os.popen("adb shell input keyevent 67")

	def screencap(self):
		filename = str(time.time())+".png"
		os.popen("adb shell screencap -p /sdcard/%s" % filename)
		time.sleep(1)
		os.popen("adb shell pull /sdcard/%s %s" % (filename, filename))
		time.sleep(1)
		os.popen("adb shell rm /sdcard/%s" % filename)

	def startapp(self):
		os.popen("adb shell am start com.smile.gifmaker/com.yxcorp.gifshow.HomeActivity")
		time.sleep(10)


def main():
	element = Element()
	event   = Event()

	for nick in ('楠楠楠楠来了来了', '然爷'):
		print(nick)
		time.sleep(3)
		#event.screencap()
		ele = element.findElementById('com.smile.gifmaker:id/toolBarSearchFirst')
		time.sleep(3)
		event.touch(ele[0], ele[1])
		time.sleep(3)
		#ele = element.findElementById('com.smile.gifmaker:id/search_layout')
		#time.sleep(3)
		#event.touch(ele[0], ele[1])
		#time.sleep(3)
		event.text(nick)
		time.sleep(3)
		ele = element.findElementById('com.smile.gifmaker:id/right_tv')
		time.sleep(3)
		event.touch(ele[0], ele[1])
		time.sleep(3)
		ele = element.findElementById('com.smile.gifmaker:id/button')
		time.sleep(3)
		event.touch(ele[0], ele[1])
		time.sleep(3)
		event.back()
		time.sleep(1)
		event.back()

		#event.swipe(540,1300,540,500)


if __name__ == '__main__':
	main()
    #try:
        #main()
    #except:
        #os.popen('adb kill-server')
        #exit(0)